﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using wdss.WebServices;

namespace wdss.GUI
{
    public partial class CheckIn : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.ExpiresAbsolute = DateTime.Now.AddDays(-1d);
            Response.Expires = -1500;
            Response.CacheControl = "no-cache";
            username_lbl.Text = (string)Session["username"];
            username_lbl.Visible = true;
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.Now);
            Response.Cache.SetNoServerCaching();

            Response.Cache.SetNoStore();

            try
            {

                if ((int)Session["role"] == 1)
                {

                    Server.Transfer("~/GUI/Login.aspx");
                }

                if ((int)Session["role"] == 3)
                {

                    Server.Transfer("~/GUI/SystemAdministrator.aspx");
                }

            }

            catch (HttpException)
            {

                Server.Transfer("~/GUI/Error.aspx");
            }

            catch (Exception)
            {

                Server.Transfer("~/GUI/Login.aspx");
            }


        }

        protected void btn_CheckIn_Click(object sender, EventArgs e)
        {
            if (ddl_ChooseDocument.SelectedItem != null)
            {
            LogService logAction = new LogService();
            string result = "Failure.";
            if (cb_Confirm.Checked)
            {
                CheckInService checkInObj = new CheckInService();
                ListItem selectedItem = ddl_ChooseDocument.SelectedItem;
                int itemvalue = Int32.Parse(selectedItem.Value);

                if (Session["userid"] != null)
                {
                    int userId = (int)Session["userid"];
                    result = checkInObj.CheckInDocument(itemvalue, userId);
                }
                else
                {
                    Server.Transfer("~/GUI/Login.aspx");
                }

            }

            //handle failure
            if (result == "Failure.")
            {
                lbl_Error.Visible = true;
                lbl_Success.Visible = false;
                lbl_Error.Text = "Could not checkIn document.";
            }
            //update documents
            if (result == "Document Checked in")
            {
                logAction.LogAction("User attempted to CheckIn the document " + ddl_ChooseDocument.SelectedValue + ".");
                lbl_Error.Visible = false;
                lbl_Success.Visible = true;
                ddl_ChooseDocument.DataBind();
            }
            else
            {
                logAction.LogAction("User attempted to CheckIn the document " + ddl_ChooseDocument.SelectedValue + " but an error prevented that.");
                lbl_Error.Text = "Selection is not confirmed.";
                lbl_Success.Visible = false;
                lbl_Error.Visible = true;
            }
        }
        else
        {
           lbl_Error.Visible = true;
           lbl_Success.Visible = false;
           lbl_Error.Text = "No document selected.";
        }
    }

  }
}

